﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


namespace DragDraw.Service
{
    public class SysMessageData
    {
        ///<summary>
        ///插入SysMessage信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public bool Insert(SysMessage sysMessage)//what if the id exist?
        {
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.StoredProcedure;
            curCommand.CommandText = "InsertSysMessage";
            curCommand.Parameters.Add("@result", SqlDbType.Int);
            curCommand.Parameters.Add("@sourceID", SqlDbType.NVarChar, 20);
            curCommand.Parameters.Add("@destinationID", SqlDbType.NVarChar, 20);
            curCommand.Parameters.Add("@tag", SqlDbType.NVarChar, 50);
            curCommand.Parameters.Add("@body", SqlDbType.NVarChar,4000);
            curCommand.Parameters.Add("@isHandled", SqlDbType.Int);
            curCommand.Parameters["@result"].Direction = ParameterDirection.Output;
            curCommand.Parameters["@sourceID"].Value = sysMessage.SourceID;
            curCommand.Parameters["@destinationID"].Value = sysMessage.DestinationID;
            curCommand.Parameters["@tag"].Value = sysMessage.Tag;
            curCommand.Parameters["@body"].Value = sysMessage.Body;
            if(sysMessage.IsHandled)
                curCommand.Parameters["@isHandled"].Value = 1;
            else
                curCommand.Parameters["@isHandled"].Value = 0;

            curCommand.Connection = SqlDataManager.OpenConnection();
            int result = 0;
            try
            {
                result = curCommand.ExecuteNonQuery();
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("SysMessageData.Insert:执行SqlCommand的时候发生错误！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
            }
            if (Convert.ToInt32(curCommand.Parameters["@result"].Value) == 0)//有没有执行sql语句？
            {
                throw new DataInsertException("SysMessageData.Insert:没有执行sql语句！", null);
            }
            if (result <= 0)//执行的sql语句是否成功？
            {
                throw new DataErrorException("SysMessageData.Insert:执行了sql语句，可是sql语句没有影响任何一条记录！", null);
            }
            return true;
        }
        ///<summary>
        ///删除所有SysMessage信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public bool DeleteAll()
        {
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.Text;
            curCommand.CommandText = "TRUNCATE TABLE SysMessage";

            curCommand.Connection = SqlDataManager.OpenConnection();
            int result = 0;
            try
            {
                result = curCommand.ExecuteNonQuery();
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("SysMessageData.DeleteAll:执行SqlCommand的时候发生错误！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
            }
            if (Convert.ToInt32(curCommand.Parameters["@result"].Value) == 0)//有没有执行sql语句？
            {
                throw new DataErrorException("SysMessageData.DeleteAll:没有执行sql语句！", null);
            }
            if (result <= 0)//执行的sql语句是否成功？
            {
                throw new DataErrorException("SysMessageData.DeleteAll:执行了sql语句，可是sql语句没有影响任何一条记录！", null);
            }
            return true;
            //还要连带删除一下FriendRelation里面的朋友关系。
        }
        ///<summary>
        ///查询SysMessage信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public List<SysMessage> QueryList(string destinationID)//if does not exist,return null
        {
            List<SysMessage> result_sysMessageList = new List<SysMessage>();
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.Text;
            curCommand.CommandText = "select * from SysMessage where DestinationID='" + destinationID + "'";

            curCommand.Connection = SqlDataManager.OpenConnection();
            bool HasResult = false;
            try
            {
                SqlDataReader reader = curCommand.ExecuteReader(CommandBehavior.Default);
                if (reader.HasRows)
                {
                    HasResult = true;
                    while (reader.Read())
                    {
                        SysMessage temp_sysMessage = new SysMessage();
                        temp_sysMessage.SourceID = reader.GetString(1);
                        temp_sysMessage.DestinationID = reader.GetString(2);
                        temp_sysMessage.Tag = reader.GetString(3);
                        temp_sysMessage.Body = reader.GetString(4);
                        int isHandled = reader.GetInt32(5);
                        if (isHandled == 1)
                            temp_sysMessage.IsHandled = true;
                        else if (isHandled == 0)
                            temp_sysMessage.IsHandled = false;
                        else
                        {
                            Console.WriteLine("SysMessage.IsHandled read Error!");
                            throw new DataErrorException("SysMessage.IsHandled read Error!",null);
                        }
                        result_sysMessageList.Add(temp_sysMessage);
                    }
                }
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("SysMessageData.QueryList:执行SqlCommand的时候发生错误！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
            }
            if (!HasResult)
            {
                throw new DataNotFoundException("SysMessageData.QueryList:顺利执行完sql语句，可是没有找到匹配的记录！", null);
            }
            return result_sysMessageList;
        }
        ///<summary>
        ///显示所有SysMessage信息
        ///</summary>
        ///<exception cref="DataErrorException">数据库执行错误</exception>
        ///<exception cref="DataNotFoundException">查找不到相应记录</exception>
        static public void Show()
        {
            Console.WriteLine("-----SysMessage Table------------------");
            SqlCommand curCommand;
            curCommand = new SqlCommand();
            curCommand.CommandType = CommandType.Text;
            curCommand.CommandText = "select * from SysMessage";
            curCommand.Connection = SqlDataManager.OpenConnection();
            try
            {
                SqlDataReader reader = curCommand.ExecuteReader(CommandBehavior.Default);
                while (reader.Read())
                {
                    Console.WriteLine("{0} {1} {2} {3} {4}", reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4), reader.GetInt32(5));
                }
            }
            catch (Exception error)
            {
                Console.WriteLine(error.Message);
                throw new DataErrorException("SysMessageData.Show:执行SqlCommand的时候发生错误！", error);
            }
            finally
            {
                SqlDataManager.CloseConnection(curCommand.Connection);
                Console.WriteLine("-------------------------------------");
            }
        }
    }
}